import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Microsoft
 * Date: 2023-03-04
 * Time: 20:46
 */
public class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        stack.push(val);
        if (minStack.empty()){
            minStack.push(val);
        }else {
            if (val <= minStack.peek()) {//这里一定要取等号，因为出栈的时候，二者都要出来，不能因为相等时候不入栈，出栈的时候出来会缺失后续的最小元素
                minStack.push(val);
            }
        }
    }

    public void pop() {
        if (!stack.empty()) {
            int val = stack.pop();
            if (val == minStack.peek()) {//如果val是Integer类型那么val和minStack.peek()的比较就要使用equals
                minStack.pop();
            }
        }
    }

    public int top() {
        if (!stack.empty()) {
            return stack.peek();
        }
        return -1;
    }

    public int getMin() {
        return minStack.peek();
    }
}
